package com.linko.intelligent.sys.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.common.util.DateUtil;
import com.linko.intelligent.sys.core.mapper.AttachMapper;
import com.linko.intelligent.sys.core.service.AttachService;
import com.linko.intelligent.sys.pojo.dto.AttachDto;
import com.linko.intelligent.sys.pojo.po.Attach;
import com.linko.intelligent.sys.pojo.query.AttachQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@Slf4j
@Service
public class AttachServiceImpl extends ServiceImpl<AttachMapper, Attach> implements AttachService {


    @Value("${file.upload-path}")
    String uploadPath;
    @Autowired
    private AttachMapper roleMapper;

    @Override
    public Boolean update(Long id, AttachDto dto) {
        Attach po = new Attach();
        BeanUtils.copyProperties(dto, po);
        po.setId(id);
        return updateById(po);
    }

    @Override
    public Attach getDetail(Long id) {
        Attach po = getById(id);
        return po;
    }

    @Override
    public List<Attach> queryList(AttachQuery query) {
        List<Attach> list = roleMapper.selectLs(query);
        return list;
    }


    @Override
    public PageInfo<Attach> queryPage(AttachQuery query) {
        PageInfo<Attach> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> queryList(query)
        );
        return pageInfo;
    }

    @Override
    public Attach save(MultipartFile file, String s, String m) {
        if (file == null || file.isEmpty()) {
            throw new RuntimeException("文件为空");
        }
        String originalName = file.getOriginalFilename(); // 获取文件名
        String suffixName = originalName.substring(originalName.lastIndexOf("."));// 获取文件的后缀名
        String fileName = UUID.randomUUID() + suffixName;
        String dirPath = "";
        if (StringUtils.isNotBlank(s)) {
            dirPath = dirPath + s + "/";
        }
        if (StringUtils.isNotBlank(m)) {
            dirPath = dirPath + m + "/";
        }
        //按天分文件夹
        dirPath = "attach/" + dirPath + DateUtil.getPatternNowDate() + "/";
        File targetFile = new File(uploadPath + dirPath + fileName);
        if (!targetFile.getParentFile().exists()) {   // 检测是否存在目录
            targetFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(targetFile.getAbsoluteFile());
            Attach attach = new Attach();
            attach.setSavePath(targetFile.getAbsolutePath());
            attach.setUrl(dirPath + fileName);
            attach.setOriginName(originalName);
            attach.setCreateTime(new Date());
            save(attach);
            return attach;
        } catch (Exception e) {
            if (targetFile.exists()) {
                targetFile.delete();
            }
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public List<Attach> saveAll(List<MultipartFile> files, String s, String m) {
        List<Attach> attaches = new ArrayList<>();
        for (MultipartFile file : files) {
            Attach attach = null;
            try {
                attach = save(file, s, m);
            } catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
            attaches.add(attach);
        }
        return attaches;
    }

}
